package com.zcf.controller;

import com.alipay.api.AlipayApiException;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zcf.common.json.Body;
import com.zcf.mapper.LogMapper;
import com.zcf.mapper.OrderMapper;
import com.zcf.mapper.UserMapper;
import com.zcf.mappers.Demo;
import com.zcf.pay.WXpay.WxNotifyParam;
import com.zcf.pojo.*;
import com.zcf.service.OrderService;
import com.zcf.utils.DateUtil;
import com.zcf.utils.Hutool;
import com.zcf.vo.out.AMapUtils;
import com.zcf.vo.out.LngLat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//import cn.hutool.core.map.MapUtil;

/**
 * <p>
 * 鍓嶇鎺у埗鍣�
 * </p>
 *
 * @author zhouzhuang
 * @since 2018-11-28
 */
@Controller
@RequestMapping("/order")
@CrossOrigin
@ResponseBody
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    OrderMapper orderMapper;

    @Autowired
    LogMapper logMapper;

    @Autowired
    UserMapper userMaper;

    // 生成订单
    @PostMapping("/myOrder")
    public Body myOrder(String userId, String diningName, String productName, Double productPrice, Integer number,
                        Double total, String diningId, String remark, String productId) {
        return orderService.myOrder(userId, diningName, productName, productPrice, number, total, diningId, remark,
                productId);
    }

    //取消订单
    @PostMapping("/backorder")
    public Body backorder(String userId, String id) {
        return orderService.backorder(userId, id);
    }

    public String DateUits(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        try {
            now = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println("当前时间：" + sdf.format(now));
        Date afterDate = new Date(now.getTime() + 2100000);// 35分钟
        System.out.println(sdf.format(afterDate));
        return sdf.format(afterDate);
    }

    /**
     * 下达订单 参数 下单用户id userId 购物车id shoppingId 优惠劵id myId 用户名 shopName 联系方式 shopPhone
     * 地址 shopAddress
     */
    @PostMapping("insertOrderInfo")
    public Body insertOrderInfo(Orders orders, String shoppingId, String myId, String predictTime, String lon,
                                String lat, String addId, String cid) {
        Address address = new Address();
        if (addId != null) {
            address.setId(addId);
            address = address.selectById();
        }
        Shopping shopping = new Shopping();
        shopping.setShoppingId(shoppingId);
        shopping.setUserId(orders.getUserId());
        shopping = shopping.selectById();
        if (shopping != null) {
            Shopp shopp = new Shopp();
            EntityWrapper<Shopp> ew = new EntityWrapper<>();
            ew.eq("shopping_id", shoppingId);
            List<Shopp> list = shopp.selectList(ew);
            if (list.size() > 0) {
                // 查询优惠劵
                MyCoupon myCoupon = new MyCoupon();
                if (myId != null && myId != "") {
                    EntityWrapper<MyCoupon> ewn = new EntityWrapper<>();
                    ewn.eq("coupon_id", myId);
                    myCoupon.setId(myId);
                    myCoupon = myCoupon.selectById();
                    if (myCoupon == null) {
                        return Body.newInstance("优惠劵不存在");
                    } else {
                        myCoupon.setUse(1);
                        myCoupon.updateById();
                    }
                }
                // 查询用户信息
                User user = new User();
                user.setId(orders.getUserId());
                user = user.selectById();
                if (user == null) {
                    return Body.newInstance("用户不存在");
                }
                // 查询商家信息
                Dining dining = new Dining();
                dining.setId(shopping.getDinId());
                dining = dining.selectById();
                if (dining == null) {
                    return Body.newInstance("商家不存在");
                }
                // 创建订单主表数据
                Orderinfo orderinfo = new Orderinfo();
                orderinfo.setAddtime(Hutool.parseDateToString());
                orderinfo.setPredictTime(this.DateUits(Hutool.parseDateToString()));
                orderinfo.setOrderInfoId(Hutool.createId());
                orderinfo.setUserId(orders.getUserId());
                orderinfo.setDinId(shopping.getDinId());
                // 个推id
                orderinfo.setCid(cid);
                if (myCoupon != null) {
                    orderinfo.setCouponMoney(myCoupon.getMoney());
                    orderinfo.setMyId(myId);
                }
                if (address.getPhone() != null) {
                    orderinfo.setPhone(address.getPhone());
                }
                orderinfo.setDinName(dining.getDiningName());
                orderinfo.setTotalMoney(Double.parseDouble(shopping.getTotal()));
                orderinfo.setStatus(0);
                orderinfo.setDinStatus(4);
                orderinfo.setDinImage(dining.getImage());
                orderinfo.setUserName(orders.getShopName());
                orderinfo.setAddress(orders.getShopAddress());

                // 根据地址获取经纬度 用户收货地址
                Demo demo = new Demo();
                System.out.println(orders.getShopAddress());
                Object s = demo.gaodeditu(orders.getShopAddress());
                String appid = s.toString();
                String lons[] = appid.split(",");
                for (int i = 0; i < lons.length; i++) {
                    String string = lons[i];
                    if (i == 0) {
                        lon = string;
                    } else {
                        lat = string;
                    }
                }
                orderinfo.setLongitude(lon);
                orderinfo.setLatitude(lat);
                Boolean falg = orderinfo.insert();
                if (falg) {
                    int count = 0;
                    for (Shopp shopp2 : list) {
                        Orders orders2 = new Orders();
                        Product products = new Product();
                        products.setId(shopp2.getProductId());
                        products = products.selectById();
                        orders2.setProductName(products.getProductName());
                        orders2.setId(Hutool.getId());
                        orders2.setShopName(user.getName());
                        orders2.setUserId(orders.getUserId());
                        orders2.setTime(Hutool.parseDateToString());
                        orders2.setDiningId(shopp2.getDinId());
                        orders2.setDiningName(dining.getDiningName());
                        orders2.setShopPhone(orders.getShopPhone());
                        orders2.setProductId(shopp2.getProductId());
                        orders2.setProductName(shopp2.getProduct_name());
                        orders2.setShopAddress(orders.getShopAddress());
                        orders2.setNumber(Integer.parseInt(shopp2.getNumber()));
                        orders2.setOrderInfoId(orderinfo.getOrderInfoId());
                        orders2.setProductPrice(shopp2.getPrice());
                        // 备注
                        orders2.setRemark(orders.getRemark());
                        // 口味
                        orders.setNotice(shopp2.getFlavor());
                        Boolean falgs = orders2.insert();
                        if (falgs) {
                            count++;
                        }
                    }
                    if (count > 0) {
                        EntityWrapper<Shopp> shopew = new EntityWrapper<>();
                        shopew.eq("shopping_id", shoppingId);
                        Shopp shopp2 = new Shopp();
                        List<Shopp> shoppList = shopp2.selectList(shopew);
                        System.err.println(shoppList.size());
                        for (Shopp shopp3 : shoppList) {
                            Shopp shopp1 = new Shopp();
                            shopp1.setShoppId(shopp3.getShoppId());
                            Boolean flag = shopp1.deleteById();
                            System.err.println(flag);
                        }
                        Shopping shopping2 = new Shopping();
                        shopping2.setShoppingId(shoppingId);
                        shopping2.deleteById();
                        return Body.newInstance(this.queryOne(orderinfo.getOrderInfoId()));
                    }
                    return Body.BODY_500;
                }
                return Body.BODY_500;
            }
            return Body.newInstance("购物车中没有商品,请先选择商品");
        }
        return Body.newInstance("购物车不存在");
    }

    /**
     * 查询单个订单信息
     */
    @PostMapping("queryOne")
    public Body queryOne(String orderInfoId) {
        Orderinfo orderinfo = new Orderinfo();
        EntityWrapper<Orderinfo> ew = new EntityWrapper<>();
        if (orderInfoId != null) {
            ew.eq("order_info_id", orderInfoId);
        }
        orderinfo = orderinfo.selectOne(ew);
        List<Object> addlist = new ArrayList<>();
        if (orderinfo != null) {
            Orders orders = new Orders();
            EntityWrapper<Orders> ews = new EntityWrapper<>();
            ews.eq("order_info_id", orderinfo.getOrderInfoId());
            List<Orders> lists = orders.selectList(ews);
            List<Orders> listsOrders = new ArrayList<>();
            if (lists.size() > 0) {
                for (Orders orders2 : lists) {
                    Product product = new Product();
                    product = product.selectById(orders2.getProductId());
                    if (product.getProductName() != null && product.getProductName() != "") {
                        orders2.setProductName(product.getProductName());
                    }
                    listsOrders.add(orders2);
                }
                orderinfo.setOrders(listsOrders);
            }
            Dining dining = new Dining();
            dining = dining.selectById(orderinfo.getDinId());
            orderinfo.setBox(dining.getBox() + "");
            addlist.add(orderinfo);
        }
        if (addlist.size() > 0) {
            return Body.newInstance(addlist);
        }
        return Body.newInstance("暂无订单");
    }

    // 指定时间增加
    public String DataFormat(String time) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        now = df.parse(time);
        Date afterDate = new Date(now.getTime() + 300000);
        return df.format(afterDate);
    }

    /**
     * 用户确认收货
     */
    @PostMapping("delivery")
    public Body delivery(String userId, String orderInfoId) {
        EntityWrapper<RidingOrder> ews = new EntityWrapper<>();
        RidingOrder ridingOrder = new RidingOrder();
        ews.eq("order_id", orderInfoId);
        ridingOrder = ridingOrder.selectOne(ews);
        if (ridingOrder == null) {
            return Body.newInstance("骑手还未接单");
        }
        if (ridingOrder != null) {
            if (ridingOrder.getType() == 1) {
                return Body.newInstance("骑手还未送达");
            }
        }

        EntityWrapper<Orderinfo> ew = new EntityWrapper<>();
        Orderinfo orderinfo = new Orderinfo();
        ew.eq("order_info_id", orderInfoId);
        ew.eq("user_id", userId);
        orderinfo = orderinfo.selectOne(ew);
        if (orderinfo.getDinStatus() == 0) {
            return Body.newInstance("商家还未接单");
        }
        orderinfo.setStatus(2);
        orderinfo.setDinStatus(2);
        Boolean falg = orderinfo.updateById();
        if (falg) {
            return Body.newInstance("确认收货完成");
        }
        return Body.newInstance("确认收货失败");
    }

    public String DataFormats(String time) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        now = df.parse(time);
        Date afterDate = new Date(now.getTime() + 900000);
        return df.format(afterDate);
    }

    @PostMapping("queryDinOrder")
    public Body queryDinOrder(String dinId, String dinStatus, HttpServletRequest request, HttpServletResponse response)
            throws ParseException, AlipayApiException {
        Orderinfo orderinfo = new Orderinfo();
        EntityWrapper<Orderinfo> ew = new EntityWrapper<>();
        ew.eq("din_id", dinId);
//		if(dinStatus.equals("1")) {
////			ew.eq("din_status",1).or("din_status", 5);
//			ew.or("din_status", 1,5);
//		}else {
        ew.eq("din_status", dinStatus);
//		}
        List<Orderinfo> list = orderinfo.selectList(ew);
        List<Object> addlist = new ArrayList<>();
        if (list.size() > 0) {
            String times = Hutool.parseDateToString();
            for (Orderinfo orderinfo2 : list) {
                String sum = DateUtil.dateDiff(orderinfo2.getAddtime(), times, "yyyy-MM-dd HH:mm:ss", "m");
                String num = DateUtil.dateDiff(orderinfo2.getAddtime(), times, "yyyy-MM-dd HH:mm:ss", "h");
                int a = Integer.parseInt(sum);
                int b = Integer.parseInt(num);
                if (orderinfo2.getDinStatus() == 0 && orderinfo2.getStatus() == 1) {
                    if (a > 5 || b > 1) {
//						Orderinfo orderinfo1 = new Orderinfo();
//						EntityWrapper<Orderinfo> en = new EntityWrapper<>();
//						en.eq("order_info_id", orderinfo2.getOrderInfoId());
//						orderinfo1 = orderinfo1.selectOne(en);
//
//						UserController userController = new UserController();
//						String prices = orderinfo1.getTotalMoney() + "";
//						Body bdoy = userController.zhifubaoTuikuan(orderinfo1.getOrderInfoId(), orderinfo1.getUserId(),
//								request, response, orderinfo1, prices);
//						if (bdoy != null) {
//							orderinfo1.setStatus(5);
//							orderinfo1.setDinStatus(3);
//							orderinfo1.updateById();
//							continue;
//						}
//						continue;
                    }
                }
                Orders orders = new Orders();
                EntityWrapper<Orders> ews = new EntityWrapper<>();
                ews.eq("order_info_id", orderinfo2.getOrderInfoId());
                List<Orders> lists = orders.selectList(ews);
                if (orderinfo2.getDinStatus() == 0) {
                    orderinfo2.setDin_status("待处理");
                } else if (orderinfo2.getDinStatus() == 1) {
                    orderinfo2.setDin_status("进行中");
                } else if (orderinfo2.getDinStatus() == 2) {
                    orderinfo2.setDin_status("已完成");
                } else if (orderinfo2.getDinStatus() == 3) {
                    orderinfo2.setDin_status("已拒绝");
                }
                for (Orders object : lists) {
                    Product product = new Product();
                    if (object.getProductId() != null) {
                        product.setId(object.getProductId());
                        product = product.selectById();
                    }
                    if (product != null) {
                        object.setProductName(product.getProductName());
                    }
                }
                if (orderinfo2.getStatus().equals("0")) {
                    String time = this.DataFormats(orderinfo2.getAddtime());
                    orderinfo2.setTime(time);
                } else {
                    String time = this.DataFormat(orderinfo2.getAddtime());
                    orderinfo2.setTime(time);
                }
                orderinfo2.setOrders(lists);
                addlist.add(orderinfo2);
            }
        }
        if (addlist.size() > 0) {
            return Body.newInstance(addlist);
        }
        return Body.newInstance("暂无订单");
    }

    /**
     * 商家或用户查询订单
     *
     * @throws ParseException
     */
    @PostMapping("queryDinOrders")
    public Body queryDinOrders(String orderInfoId, String dinId, String userId, String dinStatus)
            throws ParseException {
        Orderinfo orderinfo = new Orderinfo();
        EntityWrapper<Orderinfo> ew = new EntityWrapper<>();
        if (orderInfoId != null) {
            ew.eq("order_info_id", orderInfoId);
        }
        if (dinId != null) {
            ew.eq("din_id", dinId);
        }
        if (userId != null) {
            ew.eq("user_id", userId);
        }
        if (dinStatus != null) {
            ew.eq("din_status", dinStatus);
        }
        List<Orderinfo> list = orderinfo.selectList(ew);
        List<Object> addlist = new ArrayList<>();
        if (list.size() > 0) {
            String times = Hutool.parseDateToString();
            for (Orderinfo orderinfo2 : list) {
                Dining dining = new Dining();
                dining = dining.selectById(orderinfo2.getDinId());
                if (dining != null) {
                    orderinfo2.setDinImage(dining.getLogo());
                }

                String sum = DateUtil.dateDiff(orderinfo2.getAddtime(), times, "yyyy-MM-dd HH:mm:ss", "m");
                String num = DateUtil.dateDiff(orderinfo2.getAddtime(), times, "yyyy-MM-dd HH:mm:ss", "h");
                int a = Integer.parseInt(sum);
                int b = Integer.parseInt(num);
                if (a > 5 || b > 1) {
                    if (orderinfo2.getStatus() == 1 && orderinfo2.getDinStatus() == 0) {
                        Orderinfo orderinfo1 = new Orderinfo();
                        Orders orders = new Orders();
                        EntityWrapper<Orderinfo> en = new EntityWrapper<>();
                        en.eq("order_info_id", orderinfo2.getOrderInfoId());
                        List<Orders> OrderList = orders.selectList(en);
                        for (Orders orders1 : OrderList) {
                            Orders orders3 = new Orders();
                            orders3.setId(orders1.getId());
                            orders3.deleteById();
                        }
                        orderinfo1.setOrderInfoId(orderinfo2.getOrderInfoId());
                        orderinfo1.setStatus(5);
                        orderinfo1.setDinStatus(3);
                        orderinfo1.updateById();
                        UserController userController = new UserController();
                        String prices = "";
                        String id = "";
                        try {
                            userController.zhifubaoTuikuan(id, userId, orderinfo1, prices);
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    continue;
                }
                Orders orders = new Orders();
                EntityWrapper<Orders> ews = new EntityWrapper<>();
                ews.eq("order_info_id", orderinfo2.getOrderInfoId());
                List<Orders> lists = orders.selectList(ews);
                if (orderinfo2.getDinStatus() == 0) {
                    orderinfo2.setDin_status("待处理");
                } else if (orderinfo2.getDinStatus() == 1) {
                    orderinfo2.setDin_status("进行中");
                } else if (orderinfo2.getDinStatus() == 2) {
                    orderinfo2.setDin_status("已完成");
                } else if (orderinfo2.getDinStatus() == 3) {
                    orderinfo2.setDin_status("已拒绝");
                }
                for (Orders object : lists) {
                    Product product = new Product();
                    if (object.getProductId() != null) {
                        product.setId(object.getProductId());
                        product = product.selectById();
                    }
                    if (product != null) {
                        object.setProductName(product.getProductName());
                    }
                }
                if (orderinfo2.getStatus().equals("0")) {
                    String time = this.DataFormats(orderinfo2.getAddtime());
                    orderinfo2.setTime(time);
                } else {
                    String time = this.DataFormat(orderinfo2.getAddtime());
                    orderinfo2.setTime(time);
                }
                orderinfo2.setOrders(lists);
                if (orderinfo2 != null) {
                    Double distance = 0.0;
                    Double lat2 = Double.parseDouble(orderinfo2.getLatitude());
                    Double lng2 = Double.parseDouble(orderinfo2.getLongitude());
                    RidingOrder ridingOrder = new RidingOrder();
                    EntityWrapper<RidingOrder> ewz = new EntityWrapper<>();
                    ewz.eq("order_id", orderInfoId);
                    ridingOrder = ridingOrder.selectOne(ewz);
                    if (ridingOrder != null) {
                        Riding riding2 = new Riding();
                        EntityWrapper<Riding> ewsq = new EntityWrapper<>();
                        ewsq.eq("user_id", ridingOrder.getRidingId());
                        riding2 = riding2.selectOne(ewsq);
                        if (riding2 != null) {
                            Double lat1 = Double.parseDouble(riding2.getLatitude());
                            Double lon1 = Double.parseDouble(riding2.getLongitude());
                            LngLat start = new LngLat(lat1, lon1);
                            LngLat end = new LngLat(lng2, lat2);
                            distance = AMapUtils.calculateLineDistance(start, end);
//							distance = mapUtil.GetDistance(lat1, lon1, lat2, lng2);
                            orderinfo2.setDistance(distance);
                            orderinfo2.setQijindu(riding2.getLongitude());
                            orderinfo2.setQiweidu(riding2.getLatitude());
                        }
                    }
                }
                addlist.add(orderinfo2);
            }
        }
        if (addlist.size() > 0) {
            return Body.newInstance(addlist);
        }
        return Body.newInstance("暂无订单");
    }

    // 微信支付
    @PostMapping("/fuKuan")
    public Body fuKuan(Orderinfo orders, HttpServletRequest req, HttpServletResponse response, String myId) {
        if (myId != null) {
            MyCoupon myCoupon = new MyCoupon();
            myCoupon.setId(myId);
            Boolean flag = myCoupon.deleteById();
            if (!flag) {
                return Body.newInstance("优惠劵不存在");
            }
        }
        WXpayController wxpay = new WXpayController();
        //orders.setTotalMoney(orders.getTotalMoney());
        orders.setOrderNumber(Hutool.createId());
        orders.updateById();
        WxNotifyParam param = wxpay.initWx(req, response, orders);
        if (param == null) {
            return Body.newInstance("支付失败");
        }
        return Body.newInstance(param);
    }

    /**
     * 支付宝支付
     */
    @PostMapping("/zhifuibao")
    public Body zhifuibao(Orderinfo orders, HttpServletRequest req, String myId) {
        MyCoupon myCoupon = new MyCoupon();
        if (myId != null) {
            myCoupon.setId(myId);
            Boolean flag = myCoupon.deleteById();
            if (!flag) {
                return Body.newInstance("优惠劵不存在");
            }
        }
        ALiPayController wxpay = new ALiPayController();
        //orders.setTotalMoney(0.01);
        orders.setOrderNumber(Hutool.createId());
        orders.updateById();
        String param = wxpay.doGetCode(req, orders);
        if (param == null) {
            return Body.newInstance("支付失败");
        }
        return Body.newInstance(param);
    }

    // 商家接单，改状态，骑手接单
    @PostMapping("/jiedan")
    public Body jiedan(String id, String ridingId) {
        return orderService.jiedan(id, ridingId);
    }

    // 消费者查看订单
    @PostMapping("/shopList")
    public Body shopList(String userId, String shopStatus) throws Exception {
        return orderService.shopList(userId, shopStatus);
    }

    // 商家查看订单
    @PostMapping("/diningList")
    public Body diningList(String diningId) {
        return orderService.diningList(diningId);
    }

    /**
     * 查询骑手距离 lat1 lon1 骑手经纬度 orderInfoId 订单id
     */
    @PostMapping("addDistance")
    public Body addDistance(String orderInfoId) {
        // 计算距离
        Orderinfo orderinfo = new Orderinfo();
        orderinfo = orderinfo.selectById(orderInfoId);
        if (orderinfo != null) {
            Double distance = 0.0;
            Double lat2 = Double.parseDouble(orderinfo.getLatitude());
            Double lng2 = Double.parseDouble(orderinfo.getLongitude());
            RidingOrder ridingOrder = new RidingOrder();
            EntityWrapper<RidingOrder> ew = new EntityWrapper<>();
            ew.eq("order_id", orderInfoId);
            ridingOrder = ridingOrder.selectOne(ew);
            if (ridingOrder != null) {
                Riding riding2 = new Riding();
                EntityWrapper<Riding> ews = new EntityWrapper<>();
                ews.eq("user_id", ridingOrder.getRidingId());
                riding2 = riding2.selectOne(ews);
                if (riding2 != null) {
                    if (riding2.getLatitude() != null && !riding2.getLatitude().equals("")
                            && riding2.getLongitude() != null && !riding2.getLongitude().equals("")) {
                        Double lat1 = Double.parseDouble(riding2.getLatitude());
                        Double lon1 = Double.parseDouble(riding2.getLongitude());
                        LngLat start = new LngLat(lon1, lat1);
                        LngLat end = new LngLat(lng2, lat2);
                        distance = AMapUtils.calculateLineDistance(start, end);
                    }
                    orderinfo.setDistance(distance);
                    orderinfo.setQijindu(riding2.getLongitude());
                    orderinfo.setQiweidu(riding2.getLatitude());

                    return Body.newInstance(orderinfo);
                }
                return Body.BODY_500;
            }
            return Body.BODY_500;
        }
        return Body.BODY_500;
    }

    /**
     * 后台------查询用户订单状态
     */
    @PostMapping("querytuikuan")
    public Body querytuikuan(String status, String startTime, String endTime, String name, String phone) {
        Orderinfo orderinfo = new Orderinfo();
        EntityWrapper<Orderinfo> ew = new EntityWrapper<>();
        if (status != null && status != "") {
            ew.eq("status", status);
        }
        if (name != null) {
            ew.like("user_name", name);
        }
        if (phone != null) {
            ew.like("phone", phone);
        }
        if (startTime != null && startTime != "" && endTime != null && endTime != "") {
            ew.between("addtime", startTime, endTime);
        } else if (startTime != null && startTime != "") {
            ew.like("addtime", startTime);
        } else if (endTime != null && endTime != "") {
            ew.like("addtime", endTime);
        }
        ew.orderBy("addtime", false);
        List<Orderinfo> list = orderinfo.selectList(ew);
        List<Orderinfo> lists = new ArrayList<>();
        if (list.size() > 0) {
            for (Orderinfo orderinfo2 : list) {
                // '0:代付款 1：待收货 2：待评价 3：退款, 4评价完成
                if (orderinfo2.getStatus() == 0) {
                    orderinfo2.setLiexing("待付款");
                } else if (orderinfo2.getStatus() == 1) {
                    orderinfo2.setLiexing("待收货");
                } else if (orderinfo2.getStatus() == 2) {
                    orderinfo2.setLiexing("待评价");
                } else if (orderinfo2.getStatus() == 4) {
                    orderinfo2.setLiexing("已退款");
                } else if (orderinfo2.getStatus() == 3) {
                    orderinfo2.setLiexing("已完成");
                }
                lists.add(orderinfo2);
            }
            if (lists.size() > 0) {
                return Body.newInstance(lists);
            }
            return Body.BODY_451;
        }
        return Body.BODY_451;
    }

    /**
     * 后台------查询商家订单状态
     */
    @PostMapping("queryshangjia")
    public Body queryshangjia(String status, String name, String address) {
        Orderinfo orderinfo = new Orderinfo();
        EntityWrapper<Orderinfo> ew = new EntityWrapper<>();
        if (status != null && status != "") {
            ew.eq("din_status", status);
        }
        if (name != null && name != "") {
            ew.like("din_name", name);
        }
        if (address != "" && address != null) {
            ew.like("address", address);
        }
        List<Orderinfo> list = orderinfo.selectList(ew);
        List<Orderinfo> lists = new ArrayList<>();
        if (list.size() > 0) {
            for (Orderinfo orderinfo2 : list) {
                // 0:等待商家接单 1：进行中 2：已完成, 3 已拒绝
                if (orderinfo2.getDinStatus() == 0) {
                    orderinfo2.setLiexing("待接单");
                } else if (orderinfo2.getDinStatus() == 1) {
                    orderinfo2.setLiexing("进行中");
                } else if (orderinfo2.getDinStatus() == 2) {
                    orderinfo2.setLiexing("已完成");
                } else if (orderinfo2.getDinStatus() == 3) {
                    orderinfo2.setLiexing("拒绝接单");
                } else if (orderinfo2.getDinStatus() == 6) {
                    orderinfo2.setLiexing("已退款");
                }
                lists.add(orderinfo2);
            }
            if (lists.size() > 0) {
                return Body.newInstance(lists);
            }
            return Body.BODY_451;
        }
        return Body.BODY_451;
    }

    /**
     * 后台 ----------- 骑手查询订单状态
     *
     * @param type
     * @param startTime
     * @param endTime
     * @return
     */
    @PostMapping("queryRidingOrder")
    public Body queryRidingOrder(String type, String startTime, String endTime) {
        EntityWrapper<RidingOrder> ew = new EntityWrapper<>();
        RidingOrder ridingOrder = new RidingOrder();

        if (type != "" && type != null) {
            ew.eq("type", type);
        }

        if (startTime != null && startTime != "" && endTime != null && endTime != "") {
            ew.between("get_time", startTime, endTime);
        } else if (startTime != null && startTime != "") {
            ew.like("get_time", startTime);
        } else if (endTime != null && endTime != "") {
            ew.like("get_time", endTime);
        }

        List<RidingOrder> list = ridingOrder.selectList(ew);
        List<RidingOrder> lists = new ArrayList<>();
        if (list.size() > 0) {
            // '0：待结单 1：已接单 2:确认送达',
            for (RidingOrder ridingOrder2 : list) {
                if (ridingOrder2.getType() == 0) {
                    ridingOrder2.setTypeName("待收货");
                } else if (ridingOrder2.getType() == 1) {
                    ridingOrder2.setTypeName("配送中");
                } else if (ridingOrder2.getType() == 2) {
                    ridingOrder2.setTypeName("已完成");
                }
                lists.add(ridingOrder2);
            }
            if (lists.size() > 0) {
                return Body.newInstance(lists);
            }
            return Body.BODY_451;
        }
        return Body.BODY_451;
    }

    public static void main(String[] args) throws ParseException {
//		MapUtils mapUtil = new MapUtils();
//		double distance = mapUtil.GetDistance(117.266594, 34.144927, 266594, 34.144927);
//		System.err.println(distance + "<================");
    }

}
